#include "codon.h"
#include <stdio.h>
#include <stdlib.h>

bool SpeciesCodon::readCodonFromFile(const char *strFileName)
{
	// File input
	char strCommand[256] = {0};
	char c = '\0';
	
	FILE *inFile = fopen( strFileName, "r" );
	if( !inFile )
	{
		printf("cannot open  file!\n");
		return false;
	}
	
	//read species number
	if(fscanf( inFile, "%d", &speciesNum) != 1)
		return false;
	//while ( ( c = fgetc( inFile ) ) != EOF && ( c != '\n' ) );
	
	//read species number
	if(fscanf( inFile, "%s", speciesName) != 1)
		return false;
	//while ( ( c = fgetc( inFile ) ) != EOF && ( c != '\n' ) );
	
	Codon tmp;
	int num = 0;
	for (; ; )
	{
		strcpy(tmp.triplet, "");
		fscanf( inFile, "%s %c %lf %lf (%d)", tmp.triplet, &tmp.aminoAcid, &tmp.fraction, &tmp.frequency, &tmp.number);
		//if(tmp.triplet[0] == 0)
		if(strcmp(tmp.triplet, "") == 0)
			break;
		codonData.push_back(tmp);
	}

	fclose (inFile);
	return true;
}

void SpeciesCodon::printData()
{
	printf("speciesNumber:%d\nspeciesName:%s\n", speciesNum, speciesName);
	for (size_t i = 0; i < codonData.size(); i++)
	{
		Codon tmp = codonData[i];
		printf("%s %c %.2lf %.1lf\t(%d)\n", &tmp.triplet, tmp.aminoAcid, tmp.fraction, tmp.frequency, tmp.number);
	}
}